import H2C from "html2canvas";

export const getHtmlPicture = (el: HTMLElement | string): Promise<string> => {
    return new Promise((res, rej) => {
        const dom = typeof el === 'string' ? document.querySelector(el) as HTMLElement : el;
        if (!dom) return;
        H2C(dom, { useCORS: true }).then((canvas) => {
            canvas.toBlob((blob) => {
                if (!blob) {
                    rej("blob is null");
                    return;
                }
                const url = window.URL.createObjectURL(blob);
                res(url);
            }, "image/jpg", 1)
        });
    })
}

export const downLoadImg = (url: string, fileName="贴图自拍") => {
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = url;
    a.download = `${fileName}.jpg`;
    a.click();
};